package ulmermark.demo.rest.spring;

import java.io.ByteArrayOutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.View;

import ulmermark.demo.protobuf.OrderProtos;

public class OrderProtobufView implements View {
	
	private static final Logger LOGGER = Logger.getLogger(OrderProtobufView.class);

	@Override
	public String getContentType() {
		// TODO Auto-generated method stub
		return "application/x-protobuf";
	}


	@Override
	public void render(Map<String, ?> model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		LOGGER.debug(model);
		LOGGER.debug(request);
		LOGGER.debug(response);
		
		
		Order order = (Order) model.get("order");		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		OrderProtos.Order.newBuilder().setClientName(order.getClientName())
					.setAmount(order.getAmount()).setId(order.getId()).build().writeTo(baos);
		
		LOGGER.debug(baos.toString());
		response.getOutputStream().write(baos.toByteArray());
	}

}
